package frame.m.com.mylibrary.http;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.TimeUtils;
import android.view.Gravity;
import android.view.Window;

import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/7/7.
 */

public class LoaddingDialogHandler extends Handler {
    private QMUITipDialog load = null;

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;

    private Context context;
    private boolean cancelable;
    private final WeakReference<Context> reference;
    private ProgressCancelListener progressCancelListener;

    public LoaddingDialogHandler(Context context, ProgressCancelListener progressCancelListener,
                                 boolean cancelable) {
        super();
        this.reference = new WeakReference<Context>(context);
        this.progressCancelListener = progressCancelListener;
        this.cancelable = cancelable;
    }

    private void create() {
        if (load == null) {
            context = reference.get();
            load = new QMUITipDialog.Builder(context).setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING).create();
        }
        if (context != null) {
            load.show();
        }
    }

    public void show() {
        create();
    }

    public void dismiss() {
        context = reference.get();
        if (load != null && !((Activity) context).isFinishing()) {
            String name = Thread.currentThread().getName();
            load.dismiss();
            load = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                create();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismiss();
                break;
        }
    }
}
